package com.sky.service.impl;

import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private WorkspaceMapper workspaceMapper;

    /**
     * 查询今日运营数据
     *
     * @return {@link BusinessDataVO}
     */
    @Override
    public BusinessDataVO businessData() {
        LocalDate localDate = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(localDate,LocalTime.MAX);
        List<Map<String,Object>> list = workspaceMapper.selectBusinessData(begin,end);
        //todo 获取营业额
        List<Double> turnoverList = list.stream()
                .map(item -> (BigDecimal) item.get("turnover"))
                .map(item-> item.doubleValue())
                .collect(Collectors.toList());
        Optional<Double> optional = turnoverList.stream().reduce(Double::sum);
        Double turnoverMoney = 0.0;
        if (optional.isPresent()){
            turnoverMoney = optional.get();
        }
        //todo 有效订单数(Long)
        Optional<Long> optional1 = list.stream()
                .map(item -> (Long) item.get("sum_vaild"))
                .reduce(Long::sum);
        Integer validOrderCount = 0;
        if (optional1.isPresent()){
            validOrderCount = Math.toIntExact(optional1.get());
        }
        //todo 总订单数
        Optional<Long> optional2 = list.stream()
                .map(item -> (Long) item.get("sum_all"))
                .reduce(Long::sum);
        Integer allOrderCount = 0;
        if (optional1.isPresent()){
            allOrderCount = Math.toIntExact(optional1.get());
        }
        //todo 订单完成率
        Double orderCompletionRate = Double.valueOf(validOrderCount / allOrderCount);

        //todo 新增用户数
        /*Optional<Long> optional3 = list.stream()
                .map(item -> (Long) item.get("count_new_user"))
                .reduce(Long::sum);
        Integer newUserCount = 0;
        if (optional3.isPresent()){
            newUserCount = Math.toIntExact(optional1.get());
        }*/
        List<Long> countNewUserList = list.stream()
                .map(item -> (Long) item.get("count_new_user"))
                .collect(Collectors.toList());
        Integer newUserCount = 0;
        if (!Objects.isNull(countNewUserList.get(0))){
            /*Optional<Long> optional3 = countNewUserList.stream().reduce(Long::sum);
            if (optional3.isPresent()){
                newUserCount = Math.toIntExact(optional1.get());
            }*/
            newUserCount = Math.toIntExact(countNewUserList.get(0));
        }
        //todo 平均客单价
        Double unitPrice = turnoverMoney / validOrderCount;

        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(turnoverMoney);
        businessDataVO.setValidOrderCount(validOrderCount);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setNewUsers(newUserCount);
        

        return businessDataVO;
    }

    /**
     * 查询套餐总览
     *
     * @return {@link SetmealOverViewVO}
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map<String,Object> map = workspaceMapper.overviewSetmeals();
        Long discontinuedLong = (Long) map.get("discountined");
        int discontinued = discontinuedLong.intValue();

        Long soldLong = (Long) map.get("sold");
        int sold = soldLong.intValue();
        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return {@link DishOverViewVO}
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Map<String,Object> map = workspaceMapper.overviewDishes();
        Long discontinuedLong = (Long) map.get("discountined");
        int discontinued = discontinuedLong.intValue();

        Long soldLong = (Long) map.get("sold");
        int sold = soldLong.intValue();
        return DishOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return {@link OrderOverViewVO}
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //2,3,5,6
        Map<String,BigDecimal> map = workspaceMapper.selectOverviewOrders();
        return OrderOverViewVO
                .builder()
                .waitingOrders(map.get("waitingOrders").intValue())
                .deliveredOrders(map.get("deliveredOrders").intValue())
                .completedOrders(map.get("completedOrders").intValue())
                .cancelledOrders(map.get("cancelledOrders").intValue())
                .allOrders(map.get("allOrders").intValue())
                .build()
                ;
    }
}
